package com.hejjon.core.utils;

import cn.hutool.core.lang.Pair;
import com.hejjon.core.constant.SysConstants;
import com.hejjon.core.domain.vo.RecordRelTableInfoVO;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 集合操作工具类
 */
public class CollUtils {

    /**
     * 对象list集合的字符串属性 拼接成一个字符串
     */
    public static <T> String listJoinToStr(List<T> list, Function<T, String> fieldGetter, String separator) {
        List<String> strList = list.stream().map(fieldGetter).collect(Collectors.toList());
        return StringUtils.join(strList, separator);
    }

    public static <T> void multiListJoinToStr(T t, List<Pair<Function<T, List<String>>, BiConsumer<T, String>>> convertors) {
        for (Pair<Function<T, List<String>>, BiConsumer<T, String>> convertor : convertors) {
            Function<T, List<String>> key = convertor.getKey();
            List<String> strList = key.apply(t);
            String joinResult = StringUtils.join(strList, SysConstants.DEFAULT_SEPARATOR);
            BiConsumer<T, String> value = convertor.getValue();
            value.accept(t, joinResult);
        }
    }



}
